//
//  CertifiPhotoViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class CertifiPhotoViewModel: BaseViewModel {
    var model:CertificationStatesModel = CertificationStatesModel()
    var isChinese:Bool = false
    var name:String = ""
    var idCard:String = ""
    var selectNumber:Int = 1
    var fortImage:UIImage?
    var backImage:UIImage?
    var fortPath:String = ""
    var backPath:String = ""
    var country:String = ""
    lazy var upLoadFile:UpLoadFileRequest = {
        let api = UpLoadFileRequest ()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var seniorRequest:SeniorIdentityRequest = {
        let api = SeniorIdentityRequest ()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    func sendPhoto()  {
        CYMBShow.openIndicator(message: nil, view: self.controller?.view)
        self.request(request: upLoadFile, success: {[weak self] (result) in
            guard let array = result?["data"] as? Array<[String:Any]> else {
                return
            }
            if array.count > 1 {
                guard let fortPath:String = array.first!["relativePath"] as? String else {
                    return
                }
                guard let backPath:String = array.last!["relativePath"] as? String else {
                    return
                }
                self?.fortPath = fortPath
                self?.backPath = backPath
            }
            self?.uploadPhotoUrl()
        }) { (request) in
            CYMBShow.showMessage(request.errorMsg)
            CYMBShow.colseIndicator(view: self.controller?.view)
        }
    }
    
    func uploadPhotoUrl()  {
        self.request(request: seniorRequest, success: {[weak self] (result) in
            CYMBShow.showMessage(kText("高级认证信息提交成功"))
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
            for popView in ((self?.controller?.navigationController as! WrapNavigationController).wrapViewController?.navigationController?.viewControllers)! {
                let viewController = (popView as? WrapViewController)?.contentViewController
                if (viewController?.isKind(of: SecurityCenterViewController.self))! {
                    self?.controller?.navigationController?.popToViewController(viewController!, animated: true)
                    return
                }
            }
            self?.controller?.navigationController?.popToRootViewController(animated: true)
        }) { (response) in
            CYMBShow.showMessage(response.errorMsg)
            CYMBShow.colseIndicator(view: self.controller?.view)
        }
    }
}

extension CertifiPhotoViewModel:RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? UpLoadFileRequest {
            pramas.updateValue("idCard", forKey: "type")
//            [UIImage resetSizeOfImageDataWithSourceImage:model.photoImage maxSize:600];
            let imageData = UIImage.resetSizeOfImageData(withSourceImage: self.fortImage!, maxSize: 600)
            let file = File(fileData: imageData!, fileName: "myPortrait.jpeg", fileMIMEType: "image/jpeg")
            let backimageData = UIImage.resetSizeOfImageData(withSourceImage: self.backImage!, maxSize: 600)
            let file1 = File(fileData: backimageData!, fileName: "myPortrait1.jpeg", fileMIMEType: "image/jpeg")
            pramas.updateValue([file,file1], forKey: "files")

        }else if let _ = request as? SeniorIdentityRequest {
            pramas.updateValue(fortPath, forKey: "identityPictureOne")
            pramas.updateValue(backPath, forKey: "identityPictureTwo")
            if !self.isChinese {
                 pramas.updateValue(country, forKey: "country")
                pramas.updateValue(idCard, forKey: "credentialsCode")
                pramas.updateValue(1, forKey: "credentialsType")
                pramas.updateValue(1, forKey: "credentialsType")
                pramas.updateValue(name, forKey: "userName")
            }
            
        }
        
        return pramas
    }
}
